﻿# log4r 本地 CI 镜像脚本 (PowerShell)
# 用于在本地模拟 CI 流水线执行

param(
    [int]$BurnInIterations = 3,
    [switch]$SkipBurnIn,
    [switch]$RunBenchmark
)

$ErrorActionPreference = "Stop"

Write-Host "🔍 log4r 本地 CI 流水线" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Gray

# 1. 格式检查
Write-Host "`n📝 [1/6] 格式检查..." -ForegroundColor Yellow
cargo fmt
cargo fmt --all -- --check
if ($LASTEXITCODE -ne 0) { Write-Host "❌ 格式检查失败" -ForegroundColor Red; exit 1 }
Write-Host "✅ 格式检查通过" -ForegroundColor Green

# 2. Clippy 检查
Write-Host "`n🔎 [2/6] Clippy 检查..." -ForegroundColor Yellow
cargo clippy --all-targets --all-features -- -D warnings
if ($LASTEXITCODE -ne 0) { Write-Host "❌ Clippy 检查失败" -ForegroundColor Red; exit 1 }
Write-Host "✅ Clippy 检查通过" -ForegroundColor Green

# 3. 运行测试
Write-Host "`n🧪 [3/6] 运行测试..." -ForegroundColor Yellow
cargo test --all-features --verbose
if ($LASTEXITCODE -ne 0) { Write-Host "❌ 测试失败" -ForegroundColor Red; exit 1 }
Write-Host "✅ 测试通过" -ForegroundColor Green

# 4. Burn-in 循环
if (-not $SkipBurnIn) {
    Write-Host "`n🔥 [4/6] Burn-in 循环 ($BurnInIterations 次迭代)..." -ForegroundColor Yellow
    for ($i = 1; $i -le $BurnInIterations; $i++) {
        Write-Host "  🔥 迭代 $i/$BurnInIterations" -ForegroundColor Magenta
        cargo test --all-features --quiet
        if ($LASTEXITCODE -ne 0) {
            Write-Host "❌ Burn-in 失败 (迭代 $i)" -ForegroundColor Red
            exit 1
        }
    }
    Write-Host "✅ Burn-in 通过，未检测到 flaky 测试" -ForegroundColor Green
} else {
    Write-Host "`n⏭️ [4/6] 跳过 Burn-in" -ForegroundColor Gray
}

# 5. 文档构建
Write-Host "`n📚 [5/6] 文档构建..." -ForegroundColor Yellow
$env:RUSTDOCFLAGS = "-D warnings"
cargo doc --no-deps --all-features
if ($LASTEXITCODE -ne 0) { Write-Host "❌ 文档构建失败" -ForegroundColor Red; exit 1 }
Write-Host "✅ 文档构建通过" -ForegroundColor Green

# 6. 性能基准测试 (可选)
if ($RunBenchmark) {
    Write-Host "`n⚡ [6/6] 性能基准测试..." -ForegroundColor Yellow
    cargo bench --all-features -- --noplot
    if ($LASTEXITCODE -ne 0) { Write-Host "❌ 基准测试失败" -ForegroundColor Red; exit 1 }
    Write-Host "✅ 基准测试通过" -ForegroundColor Green
} else {
    Write-Host "`n⏭️ [6/6] 跳过基准测试 (使用 -RunBenchmark 启用)" -ForegroundColor Gray
}

Write-Host "`n━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Gray
Write-Host "✅ 本地 CI 流水线通过!" -ForegroundColor Green
Write-Host ""
Write-Host "提示: 使用以下命令运行完整检查:" -ForegroundColor Cyan
Write-Host "  .\scripts\ci-local.ps1 -BurnInIterations 10 -RunBenchmark" -ForegroundColor White
